import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtCharts 2.3
import ConfigElementType 1.0
import "qrc:/qmlComponents/ConfigManager"

ColumnLayout{
    GroupBox{
        ColumnLayout{
            Label{
                text: "UPH: " + uphHelper.uph
                font.pixelSize: 30
            }
            Label{
                text: qsTr("BondCircelTime: ") + uphHelper.lastCircelTime + " s"
                font.pixelSize: 30
            }
            Button{
                text: qsTr("Reset")
                onClicked: {
                    uphHelper.reset()
                }
            }

//// For test
//            RowLayout{
//                TextField{
//                    id: txtCount
//                    text: "10"
//                }
//                Button{
//                    text: "PushData"
//                    onClicked: {
//                        postBondData.pushData()
//                    }
//                }
//                Button{
//                    text: "PushDatas"
//                    onClicked: {
//                        tem.runSingleCmd(postBondData, "pushDatas", [txtCount.text])
//                    }
//                }
//                Button{
//                    text: "StopPushDatas"
//                    onClicked: {
//                        postBondData.stopPushDatas()
//                    }
//                }
//            }
        }
    }

    ChartView{
        title: qsTr("OffsetsStatistics")
        width: 600
        height: 300
        theme: basicConfig.chartTheme
        ValueAxis{
            id: offsets
            min: postBondData.minOffset() - 10
            max: postBondData.maxOffset() + 10
        }
        ValueAxis{
            id: index
            min: 0
            max: postBondData.maxData()
        }
        ScatterSeries{
            id: xOffsetsChart
            name: qsTr("XOffsets")
            markerSize: 7
            axisX: index
            axisY: offsets
        }
        ScatterSeries{
            id: yOffsetsChart
            name: qsTr("YOffsets")
            markerSize: 7
            axisX: index
            axisY: offsets
        }
        Connections{
            target: postBondData
            onNewData: {
                if(xOffsetsChart.count >= 100){
                    index.min += 20
                    index.max += 20
                    xOffsetsChart.removePoints(0, 20)
                    yOffsetsChart.removePoints(0, 20)
                }
                xOffsetsChart.append(index.min + xOffsetsChart.count, offsetX)
                yOffsetsChart.append(index.min + yOffsetsChart.count, offsetY)
            }
        }
    }
    RowLayout{
        ConfigRow{
            configModel: basicConfig
            configName: "strChartTheme"
            Component.onCompleted: {
                init(ConfigElementType.Other)
            }
            onChildItemInited: {
                childItem.implicitWidth = 300
            }
        }
        Button{
            text: qsTr("ClearData")
            onClicked: {
                postBondData.clearData()
                bonderWorkHolder.clearData()
                xOffsetDistributionChart.clear()
                yOffsetDistributionChart.clear()
                xOffsetsChart.clear()
                yOffsetsChart.clear()
                index.min = 0
                index.max = postBondData.maxData()
            }
        }
    }
    ChartView{
        width: 600
        height: 300
        title: qsTr("OffsetsDistribution")
        theme: basicConfig.chartTheme
        ValueAxis{
            id: offsetDistribution
            min: 0
            max: 100
        }
        ValueAxis{
            id: offsetScale
            min: postBondData.minOffset()
            max: postBondData.maxOffset()
        }
        SplineSeries{
            id: xOffsetDistributionChart
            name: qsTr("XOffsets")
            axisX: offsetScale
            axisY: offsetDistribution
        }
        SplineSeries{
            id: yOffsetDistributionChart
            name: qsTr("YOffsets")
            axisX: offsetScale
            axisY: offsetDistribution
        }
        Connections{
            target: postBondData
            onDataDistributionChanged: {
                if(maxRatio > offsetDistribution.max){
                    offsetDistribution.max = maxRatio
                }else if(maxRatio < offsetDistribution.max - 10){
                    offsetDistribution.max = maxRatio
                }
                xOffsetDistributionChart.remove(xOffsetIndex)
                yOffsetDistributionChart.remove(yOffsetIndex)
                xOffsetDistributionChart.insert(xOffsetIndex, xOffset, xOffsetRatio)
                yOffsetDistributionChart.insert(yOffsetIndex, yOffset, yOffsetRatio)
            }
            onNewDataDistribution:{
                if(maxRatio > offsetDistribution.max){
                    offsetDistribution.max = maxRatio
                }else if(maxRatio < offsetDistribution.max - 10){
                    offsetDistribution.max = maxRatio
                }

                xOffsetDistributionChart.clear()
                yOffsetDistributionChart.clear()
                for(var i = 0; i < xAxis.length; i++){
                    xOffsetDistributionChart.append(xAxis[i], yAxisXOffsetDistribution[i])
                    yOffsetDistributionChart.append(xAxis[i], yAxisYOffsetDistribution[i])
                }
            }
        }
    }
}
